package com.hluobo.library.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.hluobo.library.dao.AbstractDao;

@Transactional
public class BasicDao implements AbstractDao{

	@PersistenceContext(unitName="library")
	protected EntityManager em;
	
	public BasicDao() {
		super();
	}

	@Override
	public void save(Object entity) {
		em.persist(entity);
	}

	@Override
	public <T> void delete(Class<T> entityClass, Object primaryKey) {
		
		this.delete(entityClass, new Object[]{primaryKey});
		
	}

	@Override
	public <T> void delete(Class<T> entityClass, Object[] primaryKeys) {
		
		for(Object primaryKey : primaryKeys){
			em.remove(em.getReference(entityClass, primaryKey));
		}
		
	}

	@Override
	public void update(Object entity) {
		em.merge(entity);
	}

	@Override
	@Transactional(readOnly=true)
	public <T> T query(Class<T> entityClass, Object primaryKey) {
		return em.find(entityClass, primaryKey);
	}


	

}